<?php
namespace app\index\model;

use think\Model;
class Base extends Model
{
	public function __construct()
	{
		 parent::__construct();
    }

    /**
     * 循环结果集，将某一列由json转为数组
     * @param array $list     待处理的结果集
     * @param mixed $field    需要处理的列名(Str or Arr)
     * @return array
     */
    protected function forJsonDecode($list, $field, $fieldKey=0)
    {
        //解析列名参数
        if(is_array($field)){
            foreach($field as $fieldKey => $fieldVo){
                $list = $this->forJsonDecode($list, $fieldVo, $fieldKey);
            }
            return $list;
        }
        if(is_int($fieldKey))
            $fieldKey = $field;

        //解析结果项
        if(isset($list[$fieldKey])){
            $list[$field] = json_decode($list[$fieldKey], true);
            return $list;
        }
        if(!is_array($list))
            return $list;

        //循环处理结果集
        foreach($list as &$vo){
            if(is_array($vo))
                $vo = $this->forJsonDecode($vo, $field, $fieldKey);
        }
        unset($vo);
        return $list;
    }
}

